package com.west_traveler.find_page_fragment.cate_fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_category.entity_category_second.Entity_Cate_Fra;
import com.west_traveler.entity.entity_category.entity_category_second.Entity_Cate_Fra_CategoryContents_Recom;
import com.west_traveler.entity.entity_category.entity_category_second.Entity_Cate_Fra_CategoryContents_Recom_Content;
import com.west_traveler.ximalayafm.R;
import com.west_traveler.ximalayafm.SecondRecommendActivity;


import java.util.List;

/**
 * Created by Administrator on 12/8 0008.
 */

public class RecyclerAdapter_Cate_Recom extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private final int HANDER =1;
    private final int HOT = 2;

    private Entity_Cate_Fra data;
    private final List<Entity_Cate_Fra_CategoryContents_Recom> item_list;
    private Context context;
    private View view;

    public RecyclerAdapter_Cate_Recom(Context context,Entity_Cate_Fra data) {
        this.context = context;
        this.data = data;
        item_list = data.getCategoryContents().getList();

    }

    @Override
    public int getItemViewType(int position) {
        if (item_list.get(position).isHasMore()) {
            return HOT;
        } else {
            return HANDER;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case HANDER:
                view = LayoutInflater.from(context).inflate(R.layout.item_hander_recyc_cate_recom, parent, false);
                return new ViewHolderHander(view);
            case HOT:
                view = LayoutInflater.from(context).inflate(R.layout.item_hot_recyc_cate_recom, parent, false);
                return new ViewHolderHot(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Entity_Cate_Fra_CategoryContents_Recom recom = item_list.get(position);
        if (holder instanceof ViewHolderHander){
            List<Entity_Cate_Fra_CategoryContents_Recom_Content> data = recom.getList();
            Log.i("100","data的长度。。"+data.size());
            ((ViewHolderHander) holder).title_hander.setText(data.get(0).getTitle());
            ((ViewHolderHander) holder).stitle_hander.setText(data.get(0).getSubtitle());
            ((ViewHolderHander) holder).linear_hander.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "热门排行榜", Toast.LENGTH_SHORT).show();
                }
            });
        }
        if (holder instanceof ViewHolderHot){
            int moduleType = recom.getModuleType();
            final List<Entity_Cate_Fra_CategoryContents_Recom_Content> list = recom.getList();
            ((ViewHolderHot) holder).cate_hot.setText(recom.getTitle());
            LinearLayout linear_hot = ((ViewHolderHot) holder).linear_hot;
            linear_hot.removeAllViews();
            if (list.size()==0){
               // ((ViewHolderHot) holder).linear_item.setVisibility(View.GONE);
                 ((ViewHolderHot) holder).include.setVisibility(View.GONE);
            }else {
                //((ViewHolderHot) holder).linear_item.setVisibility(View.VISIBLE);
                ((ViewHolderHot) holder).include.setVisibility(View.VISIBLE);
            }

            if (moduleType!=4) {
                for (int i = 0; i<list.size();i++){
                    final Entity_Cate_Fra_CategoryContents_Recom_Content itemdata = list.get(i);
                    View view = LayoutInflater.from(context).inflate(R.layout.second_detial_item_recycle,null);
                    final int finalI = i;
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(context, "点击了。。"+ itemdata.getTitle(), Toast.LENGTH_SHORT).show();
//                            id = intent.getStringExtra("ID");
//                            Log.i("TAG", "接收方ID: " + id);
//                            title = intent.getStringExtra("Title");
//                            playsCounts = intent.getStringExtra("PlaysCounts");
//                            nickname = intent.getStringExtra("Nickname");
//                            coverMiddle = intent.getStringExtra("CoverMiddle");

                            Intent intent = new Intent();
                            intent.putExtra("ID",itemdata.getId()+"");
                            intent.putExtra("Title",itemdata.getTitle());
                            intent.putExtra("PlaysCounts",itemdata.getPlaysCounts()+"");
                            intent.putExtra("Nickname",itemdata.getNickname());
                            intent.putExtra("CoverMiddle",itemdata.getCoverMiddle());
                            intent.setClass(context,SecondRecommendActivity.class);
                            context.startActivity(intent);
                        }
                    });
                    Picasso.with(context).load(itemdata.getCoverSmall()).into((ImageView) view.findViewById(R.id.iv_recomment_icon));
                    TextView title = (TextView) view.findViewById(R.id.tv_title_big);
                    title.setText(itemdata.getTitle());
                    TextView intro = (TextView) view.findViewById(R.id.tv_title_small);
                    intro.setText(itemdata.getIntro());
                    TextView playsCounts = (TextView) view.findViewById(R.id.iv_play_icon_value);
                    double d = itemdata.getPlaysCounts();
                    double v = d / 10000;
                    playsCounts.setText( v+"万");
                    TextView tracks_hot = (TextView) view.findViewById(R.id.iv_collect_icon_value);

                    tracks_hot.setText(itemdata.getTracks()+"集");
                    linear_hot.addView(view);
                }
            }else if (moduleType==4){
                for (int i = 0; i<list.size();i++){
                    final Entity_Cate_Fra_CategoryContents_Recom_Content itemdata = list.get(i);
                    View view = LayoutInflater.from(context).inflate(R.layout.second_special_item_recycle,null);
                    final int finalI = i;
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(context, "点击了。。"+ itemdata.getTitle(), Toast.LENGTH_SHORT).show();
                        }
                    });
                    Picasso.with(context).load(itemdata.getCoverPathSmall()).into((ImageView) view.findViewById(R.id.iv_recomment_icon));
                    TextView title = (TextView) view.findViewById(R.id.tv_title_big);
                    title.setText(itemdata.getTitle());
                    TextView intro = (TextView) view.findViewById(R.id.tv_title_small);
                    intro.setText(itemdata.getSubtitle());
                    TextView playsCounts = (TextView) view.findViewById(R.id.iv_play_icon_value);

                    playsCounts.setText(itemdata.getFootnote());
                    linear_hot.addView(view);
                }

            }




        }


    }

    @Override
    public int getItemCount() {
        return item_list.size();
    }

    public class ViewHolderHander extends RecyclerView.ViewHolder{

        private final TextView title_hander;
        private final TextView stitle_hander;
        private final ViewPager vp_hander;
        private final LinearLayout linear_hander;

        public ViewHolderHander(View itemView) {
            super(itemView);
            linear_hander = (LinearLayout) itemView.findViewById(R.id.linear_hander_cate);
            vp_hander = (ViewPager) itemView.findViewById(R.id.vp_cate_id);
            title_hander = (TextView) itemView.findViewById(R.id.tv_title_hander);
            stitle_hander = (TextView) itemView.findViewById(R.id.tv_subtitle_hander);

        }
    }

    public class ViewHolderHot extends RecyclerView.ViewHolder{

        private final TextView cate_hot;
        private final LinearLayout linear_hot;
        private final LinearLayout linear_item;
        private final View include;


//        private final ImageView iv_cover;
//        private final TextView title_hot;
//        private final TextView intro_hot;
//        private final TextView playsCounts_hot;
//        private final TextView tracks_hot;

        public ViewHolderHot(View itemView) {
            super(itemView);
            linear_item = (LinearLayout) itemView.findViewById(R.id.ll_item_hot);
            cate_hot = (TextView) itemView.findViewById(R.id.include_cate_hot).findViewById(R.id.tv_find_fra_sound_category_id);
            linear_hot = (LinearLayout) itemView.findViewById(R.id.liner_hot_cate_id);
            include = itemView.findViewById(R.id.include_cate_hot);
//            iv_cover = (ImageView) itemView.findViewById(R.id.iv_recomment_icon);
//            title_hot = (TextView) itemView.findViewById(R.id.tv_title_big);
//            intro_hot = (TextView) itemView.findViewById(R.id.tv_title_small);
//            playsCounts_hot = (TextView) itemView.findViewById(R.id.iv_play_icon_value);
//            tracks_hot = (TextView) itemView.findViewById(R.id.iv_collect_icon_value);

        }
    }

}
